<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2018/10/26
 * Time: 14:12
 */

namespace app\admin\controller;
use think\Controller;
use app\admin\model\User as UserModel;

class User extends Controller
{
    /**
     * 用户首页
     * @return mixed
     */
    public function index(){
        return $this->fetch();
    }

    /**
     * 用户列表页面
     * @return mixed
     */
    public function userList(){
        $userList = UserModel::all();
        $this->assign("userList",$userList);
        return $this->fetch();
    }

    /**
     * 添加用户页面
     * @return mixed
     */
    public function userAdd(){
        return $this->fetch();
    }

    /**
     * 添加用户
     * @return mixed
     */
    public function add(){
        $user = new UserModel([
            'user_name'  =>  input("user_name"),
            'user_pass'  =>  input("user_pass"),
            'user_phone'  =>  input("user_phone"),
            'user_email'  =>  input("user_email"),
            'user_sex'  =>  input("user_sex"),
        ]);
        $data = $user->save();
        if($data){
            $this->success("添加成功",'userList');
        }else{
            $this->error("添加失败");
        }
    }

    /**
     * 删除用户
     */
    public function delete(){
        $id = input("id");
        $data = UserModel::destroy($id);
        if($data){
            $this->success("删除成功",'userList');
        }else{
            $this->error("删除失败");
        }
    }

    /**
     * 编辑用户页面
     * @return mixed
     */
    public function userEdit(){
        $id=input('id');
        $data = UserModel::get($id);
        $this->assign("user",$data);
        return $this->fetch();
    }

    public function edit(){
        $data = [
            'id' => input("id"),
            'user_name'  =>  input("user_name"),
            'user_pass'  =>  input("user_pass"),
            'user_phone'  =>  input("user_phone"),
            'user_email'  =>  input("user_email"),
            'user_sex'  =>  input("user_sex"),
        ];
        $result = UserModel::update($data);
        if($result){
            $this->success("修改成功",'userList');
        }else{
            $this->error("修改失败");
        }
    }
}